package cn.wolfcode.reptile;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

public class HttpClientGet {

    public static void main(String[] args) throws Exception {
        //1.指定一个url
        String url = "https://www.shicimingju.com/chaxun/list/4059.html";
        //2.创建一个默认的httpclient对象 
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //3.如果是get请求那就创建一个get对象
        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36");
        //4.发起请求
        CloseableHttpResponse execute = httpClient.execute(httpGet);
        //5.获取数据
        HttpEntity entity = execute.getEntity();
        //6.打印数据
        String html = EntityUtils.toString(entity,"utf-8");

        //System.out.println(html);

        //解析html
        Document document = Jsoup.parse(html);

        Element title = document.getElementById("zs_title");
        Element xu = document.getElementById("zs_xu");
        Element content = document.getElementById("zs_content");


        System.out.println(title.html());
        System.out.println(xu.html());
        System.out.println(content.html());
    }
}